<template>
  <div class="app-container">
    <el-card>
      <div class="base-info-title">基本信息</div>
      <div class="base-info-content">
        <div class="avatar-block">
          <el-avatar :size="80" :src="member.avatarUrl" />
          <div class="member-uuid">{{ member.memberUuid }}</div>
          <div class="last-login">上次登录：{{ member.lastLoginTime }}</div>
          <el-switch
            v-model="member.isEnabled"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
            @change="handleStatusChange"
          />
        </div>
        <div class="info-block">
          <div>昵称：{{ member.memberName }}</div>
          <div>会员名称：{{ member.memberUuid }}</div>
          <div>性别：{{ member.gender }}</div>
          <div>生日：{{ member.birthday }}</div>
          <div>地区：{{ member.region }}</div>
          <div>注册时间：{{ member.registerTime }}</div>
        </div>
      </div>
    </el-card>
    
    <el-card style="margin-top: 20px;">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="TA的积分" name="points">
          <div class="points-block">
            <div>剩余积分</div>
            <div class="points-value">{{ member.points }}</div>
          </div>
          <el-table :data="pointsList" v-loading="loading">
            <el-table-column label="操作内容" prop="remark" />
            <el-table-column label="操作时间" prop="createTime" />
            <el-table-column label="之前积分" prop="beforePoints" />
            <el-table-column label="变动积分" prop="changePoints" />
            <el-table-column label="当前积分" prop="currentPoints" />
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="TA的订单" name="orders">
          <div style="text-align: center; padding: 50px; color: #999;">暂无订单数据</div>
        </el-tab-pane>
        <el-tab-pane label="TA收货地址" name="address">
          <div style="text-align: center; padding: 50px; color: #999;">暂无收货地址</div>
        </el-tab-pane>
        <el-tab-pane label="TA的余额" name="balance">
          <div style="text-align: center; padding: 50px; color: #999;">暂无余额数据</div>
        </el-tab-pane>
        <el-tab-pane label="TA的发票" name="invoice">
          <div style="text-align: center; padding: 50px; color: #999;">暂无发票数据</div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup name="Jibenxinxi">
import { ref, onMounted, getCurrentInstance, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getMember, updateMember } from '@/api/huiyuan/member'

const { proxy } = getCurrentInstance()
const route = useRoute()
const memberId = ref(route.query.memberId)

const member = ref({})
const pointsList = ref([])
const activeTab = ref('points')
const loading = ref(false)
const isInitialized = ref(false)

async function fetchMember() {
  if (memberId.value) {
    loading.value = true
    try {
      const res = await getMember(memberId.value)
      member.value = res.data
      // 模拟积分数据
      pointsList.value = [
        {
          remark: '会员注册,赠送积分10分',
          createTime: member.value.registerTime,
          beforePoints: 0,
          changePoints: '+10.00',
          currentPoints: member.value.points || 10
        }
      ]
      // 数据加载完成后，标记为已初始化
      isInitialized.value = true
    } catch (error) {
      proxy.$modal.msgError('获取会员信息失败')
    } finally {
      loading.value = false
    }
  }
}

function handleStatusChange() {
  // 只有在页面初始化完成后才允许状态变更
  if (!isInitialized.value) {
    return
  }
  
  let text = member.value.isEnabled === 1 ? "启用" : "禁用";
  proxy.$modal.confirm(`确认要"${text}"会员 "${member.value.memberName}" 吗？`).then(() => {
    return updateMember({ ...member.value });
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(() => {
    member.value.isEnabled = member.value.isEnabled === 1 ? 0 : 1;
  });
}

onMounted(fetchMember)

watch(() => route.query.memberId, (newId, oldId) => {
  if (newId && newId !== oldId) {
    memberId.value = newId
    fetchMember()
  }
})
</script>

<style scoped>
.base-info-title { 
  font-size: 20px; 
  font-weight: bold; 
  margin-bottom: 20px; 
  color: #333;
}
.base-info-content { 
  display: flex; 
  gap: 30px;
}
.avatar-block { 
  width: 300px; 
  text-align: center; 
  background: #ff6600; 
  color: #fff; 
  border-radius: 8px; 
  padding: 20px; 
  flex-shrink: 0;
}
.member-uuid { 
  margin: 10px 0; 
  font-size: 18px; 
  word-break: break-all;
}
.last-login { 
  margin-bottom: 15px; 
  font-size: 14px;
}
.info-block { 
  flex: 1;
}
.info-block > div { 
  margin-bottom: 12px;
  font-size: 14px;
  line-height: 1.5;
}
.points-block { 
  margin: 20px 0; 
  font-size: 18px; 
  text-align: center;
}
.points-value { 
  font-size: 32px; 
  color: #ff6600; 
  font-weight: bold;
  margin-top: 10px;
}
</style>
